public class Test {
    public int longestOnes(int[] nums, int k) {
        int ret = 0;
        for(int left = 0, right = 0, zero = 0; right < nums.length; right++){
            if(nums[right] == 0) {
                zero++;//进窗口
            }
            while(zero > k) {
                if(nums[left++] == 0) {
                    zero--;//出窗口
                }
            }
            ret = Math.max(ret, right - left + 1);//更新结果
        }
        return ret;
    }

    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int a : nums) {
            sum += a;
        }
        int target = sum - x;
        //处理细节
        if(target < 0) {
            return -1;
        }

        int ret = -1;
        for(int left = 0, right = 0, tmp = 0; right < nums.length; right++) {
            tmp += nums[right];//进窗口
            while(tmp > target) {//判断
                tmp -= nums[left++];//出窗口
            }
            if(tmp == target) {
                ret = Math.max(ret, right - left + 1);
            }
        }
        if(ret == -1) {
            return -1;
        }else {
            return nums.length - ret;
        }
    }
}
